package GUITOOl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * The type MyBirthday chooseBox.
 *
 * @author dzr, wyc, zry
 * @version 1.0
 */
public class MyBirthdayChooseBox{
    private Color bgColor=new Color(255,255,255);
    private MyLineBorder textLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
    private String tempYear;
    private String tempMonth;
    private String tempDay;
    /**
     * The Com box year.
     */
    JComboBox comBox_year;
    /**
     * The Com box month.
     */
    JComboBox comBox_month;
    /**
     * The Com box day.
     */
    JComboBox comBox_day;
    /**
     * The Year.
     */
    JLabel year=new JLabel("年");
    /**
     * The Month.
     */
    JLabel month=new JLabel("月");
    /**
     * The Day.
     */
    JLabel day=new JLabel("日");
    /**
     * The Box right birth.
     */
    Box boxRight_birth;

    /**
     * Instantiates a new MyBirthday chooseBox.
     *
     * @param tempYear  the temp year
     * @param tempMonth the temp month
     * @param tempDay   the temp day
     */
    public MyBirthdayChooseBox(String tempYear, String tempMonth, String tempDay) {
        this.tempYear = tempYear;
        this.tempMonth = tempMonth;
        this.tempDay = tempDay;
    }

    /**
     * Get birthday chooseBox.
     *
     * @return the box
     */
    public Box getBirthdayChooseBox(){
        ComboBoxListener comboBoxListener_year;
        ComboBoxListener comboBoxListener_month;
        ComboBoxListener comboBoxListener_day;

        comBox_year=new JComboBox();
        comBox_month=new JComboBox();
        comBox_day=new JComboBox();
        comBox_month.setBackground(bgColor);
        comBox_year.setBackground(bgColor);
        comBox_day.setBackground(bgColor);
        comBox_year.setBorder(textLineBorder);
        comBox_month.setBorder(textLineBorder);
        comBox_day.setBorder(textLineBorder);
        for(int i=1994;i<=2021;i++) {
            comBox_year.addItem(String.valueOf(i));
        }
        for(int i=1;i<=12;i++) {
            comBox_month.addItem(String.valueOf(i));
        }
        for(int i=1;i<=31;i++) {
            comBox_day.addItem(String.valueOf(i));
        }
        comBox_year.setSelectedItem(tempYear);
        comBox_month.setSelectedItem(tempMonth);
        comBox_day.setSelectedItem(tempDay);

        comboBoxListener_year=new ComboBoxListener();
        comBox_year.addItemListener(comboBoxListener_year);
        comboBoxListener_month=new ComboBoxListener();
        comBox_month.addItemListener(comboBoxListener_month);
        comboBoxListener_day=new ComboBoxListener();
        comBox_day.addItemListener(comboBoxListener_day);

        boxRight_birth=Box.createHorizontalBox();
        boxRight_birth.add(comBox_year);
        boxRight_birth.add(year);
        boxRight_birth.add(comBox_month);
        boxRight_birth.add(month);
        boxRight_birth.add(comBox_day);
        boxRight_birth.add(day);

        return boxRight_birth;
    }

    /**
     * The type Combo box listener.
     *
     * @author dzr
     * @version 1.0
     */
    class ComboBoxListener implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            tempYear=(String)comBox_year.getSelectedItem();
            tempMonth=(String)comBox_month.getSelectedItem();
            tempDay=(String)comBox_day.getSelectedItem();
            if(e.getStateChange()==ItemEvent.SELECTED) {
                try {
                    int add=0;
                    int year=Integer.parseInt(tempYear);
                    int month=Integer.parseInt(tempMonth);
                    if((year%4==0 && year%100!=0) || year%400==0) {
                        add=1;
                    }
                    switch(month) {
                        case 1:case 3:case 5:case 7:case 8:case 10:case 12:
                        {
                            boxRight_birth.remove(comBox_day);
                            boxRight_birth.remove(day);
                            comBox_day=new JComboBox<String>();
                            boxRight_birth.add(comBox_day);
                            boxRight_birth.add(day);
                            for(int i=1;i<=31;i++) {
                                comBox_day.addItem(String.valueOf(i));
                            }
                            break;
                        }
                        case 2:{
                            boxRight_birth.remove(comBox_day);
                            boxRight_birth.remove(day);
                            comBox_day=new JComboBox<String>();
                            boxRight_birth.add(comBox_day);
                            boxRight_birth.add(day);
                            for(int i=1;i<=28+add;i++) {
                                comBox_day.addItem(String.valueOf(i));
                            }
                            break;
                        }
                        default:{
                            boxRight_birth.remove(comBox_day);
                            boxRight_birth.remove(day);
                            comBox_day=new JComboBox<String>();
                            boxRight_birth.add(comBox_day);
                            boxRight_birth.add(day);
                            for(int i=1;i<=30;i++) {
                                comBox_day.addItem(String.valueOf(i));
                            }
                            break;
                        }
                    }
                }
                catch(Exception e1){
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * Gets com box year.
     *
     * @return the com box year
     */
    public JComboBox getComBox_year() {
        return comBox_year;
    }

    /**
     * Gets com box month.
     *
     * @return the com box month
     */
    public JComboBox getComBox_month() {
        return comBox_month;
    }

    /**
     * Gets com box day.
     *
     * @return the com box day
     */
    public JComboBox getComBox_day() {
        return comBox_day;
    }
}
